package com.xiaohudie.JDBC;

/**
 * @Author xiao hu die
 * @Date 2022/3/12 21:43
 * @Version 1.0
 */

import java.sql.*;

/**调用反射机制
 * 让Driver中的静态代码块执行
 * 静态代码块在类加载的时候就执行
 *  static {
 *         try {
 *             DriverManager.registerDriver(new Driver());
 *         } catch (SQLException var1) {
 *             throw new RuntimeException("Can't register driver!");
 *         }
 *     }*/

public class TestJdbcClass {
    public static void main(String[] args) {
        Connection connection =null;
        Statement  statement =null;
        ResultSet resultSet =null;
        try {
            /**注册驱动*/
            Class.forName("com.mysql.jdbc.Driver");
            /**获取连接*/
            try {
                connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/liu?characterEncoding=utf-8", "root", "123456");
                /**获取数据库操作对象*/
                statement = connection.createStatement();
                /**得到sql语句*/
                String InsertSql= "INSERT INTO user (id,name,phone) VALUES (7,'陈唯怡','13695478526')";
                /**执行sql语句*/
                int i = statement.executeUpdate(InsertSql);
                System.out.println(i);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
